package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;
import com.edais.util.ThirdPartyEnvUtil;

@Controller
@RequestMapping("/express")

public class ExpressAction implements Serializable {
	
	private static final Log logger = LogFactory.getLog(ExpressAction.class);
	
	/**
	 * 根据快递单号查询快递信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryExpressInfo")
	public void getUserBankCardInfo(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		// 物流公司
		String com = paramsMap.get("com");
		// 单号
		String num = paramsMap.get("num");

		if (StringUtils.isBlank(com)) {
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,
					"物流公司不能为空！");
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
		if (StringUtils.isBlank(num)) {
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,
					"快递单号不能为空！");
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
		try {
			 //第三方工程请求url
			 String url = ThirdPartyEnvUtil.getValue("kuaidiQueryUrl");
			 String requestParam = "num="+num+"&com="+com;
			 String result = CommonUtil.sendPostByHttp(url, requestParam);
			 JSONObject res = JSONObject.fromObject(result);
			 res.put("rescode", Consts.SUCCESS_CODE);
			 res.put("resmsg", "");
			 res.put("resmsg_cn", "");
			 CommonUtil.responseJson(res.toString(), response);
		} catch (Exception e) {
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
}
